package b4.model
{
	import b4.ApplicationFacade;
	import b4.model.vo.DataVO;
	import b4.model.vo.player.Equip;
	import b4.model.vo.player.Obj;
	import b4.model.vo.player.Pack;
	import b4.model.vo.player.Role;
	import b4.util.GameProfile;
	import b4.view.ChatDialogMediator;
	import b4.view.HumanBeingMediator;
	import b4.view.PackageDialogMediator;
	import b4.view.SmithyMediator;
	
	import flash.events.*;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.smartbird.Message;
	import org.smartbird.SBSEvent;
	import org.smartbird.SmartBirdClient;
	import org.smartbird.UpStreamDataObject;

    public class SmartBirdExtension extends Proxy implements IProxy
    {
		public static const NAME:String	= 'SmartBirdExtension';
        private var smartbirdClient:SmartBirdClient;

        public function SmartBirdExtension(smartbirdClient:SmartBirdClient, data:DataVO) 
        {
        	super(SmartBirdExtension.NAME, data);
            this.smartbirdClient = smartbirdClient;
            smartbirdClient.addEventListener(SBSEvent.CONNECTION, onConnect);
            smartbirdClient.addEventListener(SBSEvent.LOGIN, onLogin);
            smartbirdClient.setExtension(this);
        }
        
        public function connect():void
        {
            smartbirdClient.connect(GameProfile.HOST_NAME, GameProfile.HOST_PORT);
        }
        
        public function sendMessage(message:String, type:int):void
        {
        	smartbirdClient.sendMessage(message, type);
        }
        
        public function onConnect(event:SBSEvent):void 
        {
            smartbirdClient.send("tgw_l7_forward\r\nHost: 3366.app100659558.twsapp.com:8001\r\n\r\n");
            smartbirdClient.login(17);
//            smartbirdClient.send("{method:getUser}\r\n");
        }
        
        public function onGetMessage(jsonObj:Object):void
        {
        	var message:Message = new Message();
        	message.type = jsonObj.data.message.type;
        	message.message = jsonObj.data.message.message;
        	vo.messages.push(message);
        	vo.newMessage = message;
        	sendNotification(ChatDialogMediator.GET_NEW_MESSAGE);
        }
        
        public function onGetMainRole(jsonObj:Object):void
        {
        	if (vo.town.myRole == null)
        	{
        		var role:Role = new Role();
        		role.loadbyObject(jsonObj.data.role);
        		vo.town.myRoleId = role.id;
        		vo.town.roleList[role.id] = role;	
        	}
        	else
        	{
        		vo.town.myRole.loadbyObject(jsonObj.data.role);
        	}        	
        	sendNotification(ApplicationFacade.MAINROLE_LOADED);
        }
        
        public function onTakeOnEquip(jsonObj:Object):void
        {
        	if (jsonObj.res == 0)
        	{
        		onGetMainRole(jsonObj);
        	}
        	sendNotification(HumanBeingMediator.UPON_EQUIPMENT_BACK, jsonObj.res);
        }
       
        
        public function onLogin(event:SBSEvent):void
        {
			getMainRole();
        }
        
        public function getMainRole():void
        {
        	var upDo:UpStreamDataObject = new UpStreamDataObject();
        	upDo.method = "getMainRole";
        	smartbirdClient.sendUpStreamDataObject(upDo);
        }
        
        public function takeOnEquip(id:int):void
        {
        	var upDo:UpStreamDataObject = new UpStreamDataObject();
        	upDo.method = "takeOnEquip";
        	upDo.data = {"id":id}
        	smartbirdClient.sendUpStreamDataObject(upDo);
        }
        
        public function blackSmithyOperation(data:Object):void
        {
        	var upDo:UpStreamDataObject = new UpStreamDataObject();
        	upDo.method = data.method;
        	upDo.data = {"objList":data.objList, "equipList":data.equipList};
        	smartbirdClient.sendUpStreamDataObject(upDo);
        }
        
        public function onStrengthenEquip(jsonObj:Object):void
        {    	
        	var equip:Equip;
        	if ((jsonObj.res == 0 || jsonObj.res == 103) && jsonObj.data.hasOwnProperty("equipList"))
        	{
        		for ( var temStr:String in jsonObj.data.equipList)
        		{
        			if (equip == null)
        			{
        				equip = new Equip();
						equip.loadbyObject(jsonObj.data.equipList[temStr]);
						vo.me.pack.equipList[equip.id] = equip;
						vo.me.pack.smithyEquipList[equip.id] = equip;	
        			}
        		}
        	}
        	//暂时做一下容错
        	if (jsonObj.hasOwnProperty("objList"))
        	{
	        	for each(var temp:Object in jsonObj.objList)
	        	{
	        		var obj:Obj = new Obj();
					obj.loadbyObject(temp);
					vo.me.pack.objList[obj.index] = obj;
	        	}
        	}
			var result:Object = {"res": jsonObj.res};
        	sendNotification(SmithyMediator.STRENGTHEN_EQUIP_BACK, result);
        }
        
        public function onBreakEquip(jsonObj:Object):void
        {
        	var newAddedObjList:Array = new Array();
        	var result:Object={"rc":jsonObj.res};
        	if (jsonObj.res == 0 )
        	{
        		if (jsonObj.data.hasOwnProperty("equipList"))
	        	{
	        		for (var idKey:String in jsonObj.data.equipList)
		        	{
		        		delete vo.me.pack.equipList[idKey];
		        	}	
	        	}
	        	if (jsonObj.data.hasOwnProperty("newAddedObjList"))
	        	{
	        		for each(var temp:Object in jsonObj.data.newAddedObjList)
		        	{
		        		var newObj:Obj = new Obj();
						newObj.loadbyObject(temp);
						newAddedObjList.push(newObj);
		        	}
		        	if(newAddedObjList.length > 0)
		        	{
		        		vo.me.pack.reFreshSmithyItem(newAddedObjList);
		        	}
	        	}
	        	if (jsonObj.data.hasOwnProperty("objList"))
	        	{
	        		for each(var tempObj:Object in jsonObj.data.objList)
		        	{
		        		var obj:Obj = new Obj();
						obj.loadbyObject(tempObj);
						vo.me.pack.objList[obj.index] = obj;
		        	}
	        	}
        	}
        	sendNotification(PackageDialogMediator.REFRESH_PACKAGE);
        	sendNotification(SmithyMediator.SHOW_BREAK_EQUIP_RESULT, result)
        }
        
        public function onRefineEquip(jsonObj:Object):void
        {
        	var result:Object = {"rc":jsonObj.res};
        	if (jsonObj.res == 0 && jsonObj.data.hasOwnProperty("equipList"))
        	{
        		for each(var tempEquip:Object in jsonObj.data.equipList)
	        	{
	        		var equip:Equip = new Equip();
					equip.loadbyObject(tempEquip);
					vo.me.pack.equipList[equip.id] = equip;
					vo.me.pack.reFreshSmithyItem([equip])
					
	        	}
        	}
        	if(jsonObj.res == 0 && jsonObj.data.hasOwnProperty("objList"))
        	{
        		for each(var tempObj:Object in jsonObj.data.objList)
	        	{
	        		var obj:Obj = new Obj();
					obj.loadbyObject(tempObj);
					vo.me.pack.objList[obj.index] = obj;
	        	}
        	}
        	sendNotification(PackageDialogMediator.REFRESH_PACKAGE);
        	sendNotification(SmithyMediator.SHOW_REFINE_EQUIP_RESULT, result);
        }
        public function getPackage():void
        {
        	var upDo:UpStreamDataObject = new UpStreamDataObject();
        	upDo.method = "getPackage";
        	smartbirdClient.sendUpStreamDataObject(upDo);
        }
        
        public function onGetPackage(jsonObj:Object):void
        {
        	var pack:Pack = vo.me.pack;
        	pack.clearPackage();
        	for (var idStr:String in jsonObj.data.equipList)
        	{
				var equip:Equip = new Equip();
				equip.loadbyObject(jsonObj.data.equipList[idStr]);
				pack.equipList[equip.id] = equip;
        	}
        	for (idStr in jsonObj.data.objList)
        	{
				var obj:Obj = new Obj();
				obj.loadbyObject(jsonObj.data.objList[idStr]);
				pack.objList[obj.index] = obj;
        	}
        	sendNotification(ApplicationFacade.PACKAGE_LOADED);
        }
		
		public function get vo():DataVO
		{
			return data as DataVO;
		}
    }
}



